<?php
/**
 *
 * 秒杀控制器
 * @version 2015101415
 * @author Justin <justin@jipu.com> & <ezhu@jipukeji.com>
 */

namespace Admin\Controller;
use Think\Cache;

class SeckillActivityController extends AdminController{
    
    
    function _before_index_display(&$lists){
        foreach($lists as &$vo){
            $item_ids_arr = str2arr($vo['item_ids']);
            foreach($item_ids_arr as $item){
                $vo['item_info'][] = get_item_info($item);
            }
        }
    }
    
    
    /**
     * 加入redis缓存
     */
    public function setRedisData($data){
        $redis = Cache::getInstance('Redis');
        $redis->set('invoice_'.$data['id'],$data);
    }


    public function add(){
        $this->meta_title = '新增'.$this->meta[CONTROLLER_NAME];
        $store_grade = M('StoreGrade')->order('id desc')->select();
        $this->assign('store_grade',$store_grade);
        file_exists(T()) ? $this->display() : $this->display('edit');
    }

    public function edit(){
        $id = I('request.id');
        if(empty($id)){
            $this->error('参数不能为空！');
        }
        $store_grade = M('StoreGrade')->order('id desc')->select();
        $this->assign('store_grade',$store_grade);
        $this->data = D('SeckillActivity')->detail($id);
        $this->meta_title = '编辑'.$this->meta[CONTROLLER_NAME];
        //display前置操作
        method_exists($this, '_before_edit_display') && $this->_before_edit_display();
        $this->display();
    }

    public function update(){
        if(IS_POST && IS_AJAX){
            $id = I('id');
            $saveData['name'] = I('name');
            $saveData['start_time'] = strtotime(I('start_time'));
            $saveData['expire_time'] = strtotime(I('expire_time'));
            $saveData['store_amount'] = I('store_amount');
            $saveData['store_grade']  = implode(',',I('store_grade'));
            $saveData['rule'] = I('rule');
            $saveData['item_num'] = I('item_num');
            if(!$saveData['item_num']>0){
                $this->error('请输入正确的上传商品数');
            }
            if(!$saveData['store_grade']){
                $this->error('请选择店铺等级');
            }
            if($saveData['expire_time']<$saveData['start_time']){
                $this->error('活动结束时间不能小于开始时间');
            }
            $this->data = D('SeckillActivity')->detail($id);
            if(!empty($id)){
                $rst = M('SeckillActivity')->where(array('id'=>$id))->save($saveData);
                $type = '编辑';
            }else{
                $rst = M('SeckillActivity')->add($saveData);
                $type = '新增';
            }
            ($rst !== false) ? $this->success($type.'成功',U('index') ) : $this->error($type.'失败');
            exit;
        }
            parent::update();
    }
    
    public function detail(){
        $id = I('id');
        $data = M('SeckillActivity')->where(array('id'=>$id))->find();
       // var_dump($data);die;
        $where['act_id'] = $id;
        $where['status']= array('neq',-1);
        $lists = M('Seckill')->where($where)->select();

       // dump($lists);die;
        foreach($lists as $k => $val){
            $map['store_id'] = $val['store_id'];
            $list = M('store')->where($map)->find();
            $lists[$k]['store_name'] = $list['store_name'];
            $lists[$k]['grade_id'] = $list['grade_id'];
            $lists[$k]['item_num'] = count(explode(',',$val['item_ids']));
            $lists[$k]['member_id'] = $list['member_id'];
        }
        //dump($list);die;
        //dump($lists);die;
        $data['id'] = $id;
        $this->assign('id',$id);
        $this->assign('lists',$lists);
        $this->assign('data',$data);
        $this->display();
    }

    public function setStatus()
    {
        $ids = I('request.ids');
        $status = I('request.status');
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        $map['id']  = array('in',$ids);
        $data['status'] = $status;
       if($data['status'] == 1){
       $rs =  M('Seckill')->where($map)->save($data);
        if($rs){
            $lists = M('Seckill')->where(array('status' => 1, 'id' => array('in', $ids)))->select();
            foreach ($lists as $k => $val) {
                $where['seckill_id'] = $val['id'];
                $where['status'] = array('in', array(1, 2));
                M('SeckillItem')->where($where)->setField('status', 1);
                $this->success('操作成功');
            }
        }
       }else{
          $rst =  M('Seckill')->where($map)->save($data);
            if($rst){
                $this->success('操作成功');
            }
       }
    }

    public function setItemStatus(){
        $ids = I('request.ids');
        $status = I('request.status');
     //   dump($ids);die;
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        $map['item_id'] = $ids;
        $data['status'] = $status;
     //   dump($ids,$data);die;
        M('SeckillItem')->where($map)->save($data) ? $this->success('操作商品成功') : $this->error('操作商品失败');

    }


    public function setStatuss()
    {
        $ids = I('request.ids');
        $status = I('request.status');
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        $map['id']  = array('in',$ids);
        $data['status'] = $status;
        D('Shop/Seckill')->where(array('act_id'=>$ids))->save($data);
        $list = D('Shop/Seckill')->where(array('act_id'=>$ids))->select();
        foreach ($list as $k=>$val){
            $where['seckill_id'] = $val['id'];
            D('Shop/SeckillItem')->where($where)->save($data);
        }
        M('SeckillActivity')->where($map)->save($data) ? $this->success('操作成功') : $this->error('操作失败');
    }

    public function commodity(){
        //获取平台秒杀活动ID
        $id = I('id');
        //查询活动下商家的秒杀活动
        $list = M('Seckill')->where(array('act_id'=>$id))->select();
        //获取此活动的所有商品
        foreach($list as $k => $val){
            $map['seckill_id'] = $val['id'];
            $lists[] = M('SeckillItem')->where($map)->select();
            //$arr = $lists[$k]['item_id'];
            }
        $i=0;
        foreach ($lists as $k =>$val){
            foreach($val as $key => $v){
                 $arr[$i]['item_id'] = $v['item_id'];
                 $arr[$i]['seckill_id'] = $v['seckill_id'];
                $i++;
             }
        }

        foreach($arr as $key => $val){
          //  $items= array();
            $items[$key] = M('Item')->where(array('id'=>$val['item_id']))->find();
            $items[$key]['seckill_id'] = $val['seckill_id'];
        }

        foreach ($items as $k=>$val){
            $map1['item_id'] = $val['id'];
            $map1['seckill_id'] =$val['seckill_id'];
            $seckill_item = M('SeckillItem')->where($map1)->find();
            $items[$k]['item_stock'] = $seckill_item['item_stock'];
            $items[$k]['item_price'] = $seckill_item['item_price'];
            $items[$k]['seckill_status'] = $seckill_item['status'];
        }
     //   dump($items);
        //获取商品所属店铺
        $store = M('store')->where(array('status'=>1))->getField('store_id,store_id,store_name,member_id');
        if($items){
            foreach($list as &$v){

                $v['category'] = '';
                //分类处理
                if($v['cid_1']){
                    $v['category'] = M('item_category')->where(array('id'=>$v['cid_1']))->getField('name');
                }
                if($v['cid_2']){
                    $v['category'] .= '/'. M('item_category')->where(array('id'=>$v['cid_2']))->getField('name');
                }
                if($v['cid_3']){
                    $v['category'] .= '/'. M('item_category')->where(array('id'=>$v['cid_3']))->getField('name');
                }
            }
        }
        //供应商
        //$this->lists_supplier = D('AuthGroup')->memberInGroup(C('SUPPLIER_GROUP_ID'));
        $supMap['status'] = 1;
        $field = 'id,supplier_id,name,link_name';
        $this->lists_supplier = M('supplier')->where($supMap)->field($field)->select();

        //记录当前列表页的Cookie
        Cookie('__forward__',$_SERVER['REQUEST_URI']);
        // 增加排序
        $this->setListOrder();
        //模板输出变量赋值
        $this->assign('list', $list);
        $this->assign('store',$store);
        $this->meta_title = '商品列表';
        $this->assign('items',$items);
        $this->display();
    }

}

